package com.leyou.item.service;

import com.leyou.common.exception.domain.ExceptionEnum;
import com.leyou.common.exception.domain.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.domain.SpecGroup;
import com.leyou.item.domain.SpecParam;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class SpecService {

    @Autowired
    private SpecGroupMapper groupMapper;

    @Autowired
    private SpecParamMapper paramMapper;

    public List<SpecGroupDTO> findSpecGroupByCid(Long id) {
        SpecGroup record = new SpecGroup();
        record.setCid(id);
        List<SpecGroup> specGroups = groupMapper.select(record);
        if(CollectionUtils.isEmpty(specGroups)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(specGroups, SpecGroupDTO.class);
    }

    public List<SpecParamDTO> findSpecParams(Long gid, Long cid, Boolean searching) {
        if(cid==null && gid==null){
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        SpecParam record = new SpecParam();
        record.setCid(cid);
        record.setGroupId(gid);
        record.setSearching(searching);
        List<SpecParam> specParams = paramMapper.select(record);
        if(CollectionUtils.isEmpty(specParams)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(specParams, SpecParamDTO.class);
    }

    public List<SpecGroupDTO> findSpecGroupAndParamByCid3(Long id) {
        //根据分类的第三级id查询规格组的集合
        List<SpecGroupDTO> specGroupDTOS = findSpecGroupByCid(id);
        //根据分类的第三级id查询规格参数的集合
        List<SpecParamDTO> specParamDTOS = findSpecParams(null, id, null);
        //对规格参数的集合的集合进行分组，分组的条件是规格组的id
        Map<Long, List<SpecParamDTO>> specParamMap =
                specParamDTOS.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        //变量规格组的集合
        specGroupDTOS.forEach(specGroupDTO -> {
            specGroupDTO.setParams(specParamMap.get(specGroupDTO.getId()));
        });

        //遍历规格组的集合
//        specGroupDTOS.forEach(specGroupDTO -> {
//            //变量规格参数的集合
//            specParamDTOS.forEach(specParamDTO -> {
//                //比对当前规格参数的分类id与当前规格组的id是否一直，判断当前规格参数属于哪个分类
//                if(specParamDTO.getGroupId()==specGroupDTO.getId()){
//                    //如果属于当前规格组，则把当前规格参数对象添加到规格组的规格参数集合中去
//                    specGroupDTO.getParams().add(specParamDTO);
//                }
//            });
//        });

        //在循环内执行数据库操作效率有问题，不推荐
//        specGroupDTOS.forEach(specGroupDTO -> {
//            List<SpecParamDTO> specParamDTOS = findSpecParams(specGroupDTO.getId(), null, null);
//            specGroupDTO.setParams(specParamDTOS);
//        });
        return specGroupDTOS;
    }
}
